package com.tdkj.service.shop.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.tdkj.dao.shop.IBeanShelfDefaultDao;
import com.tdkj.model.shop.BeanShelfDefault;
import com.tdkj.service.BaseService;
import com.tdkj.service.shop.IBeanShelfDefaultService;

@Component("shopShelfDefaultService")
public class BeanShelfDefaultServiceImpl extends BaseService implements
		IBeanShelfDefaultService {
	
	private final static int INIT_DEFAULT_QUANTITY= 5;

	private IBeanShelfDefaultDao beanShelfDefaultDao;
    
	@Resource
	public void setBeanShelfDefaultDao(IBeanShelfDefaultDao beanShelfDefaultDao) {
		this.beanShelfDefaultDao = beanShelfDefaultDao;
	}

	public IBeanShelfDefaultDao getBeanShelfDefaultDao() {
		return beanShelfDefaultDao;
	}

	@Override
	public void initShelfDefault() {
		List<BeanShelfDefault> beans = beanShelfDefaultDao.findAll(BeanShelfDefault.class);
		if(beans == null || beans.size() == 0){
			BeanShelfDefault bean = new BeanShelfDefault();
			bean.setQuantity(INIT_DEFAULT_QUANTITY);
			beanShelfDefaultDao.save(bean);
		}
	}

	@Override
	public void updateShelfDefault(Integer quantity) {
		List<BeanShelfDefault> beans = beanShelfDefaultDao.findAll(BeanShelfDefault.class);
		BeanShelfDefault bean = null;
		if(beans == null || beans.size() == 0){
			bean = new BeanShelfDefault();
			bean.setQuantity(INIT_DEFAULT_QUANTITY);
			beanShelfDefaultDao.save(bean);
		}else{
			bean = beans.get(0);
		}
		bean.setQuantity(quantity);
	}

	@Override
	public BeanShelfDefault getShelfDefault() {
		List<BeanShelfDefault> beans = beanShelfDefaultDao.findAll(BeanShelfDefault.class);
		BeanShelfDefault bean = null;
		if(beans == null || beans.size() == 0){
			bean = new BeanShelfDefault();
			bean.setQuantity(INIT_DEFAULT_QUANTITY);
			beanShelfDefaultDao.save(bean);
		}else{
			bean = beans.get(0);
		}
		return bean;
	}

}
